﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FuQinTube
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        List<Tube> tubes;
        private string currentExcel ;//当前excel名字
        ExcelHandle excelHandle;
        private void Form1_Load(object sender, EventArgs e)
        {
            excelHandle = new ExcelHandle();

            tubes = new List<Tube>();
        }

        private async void label1_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            foreach (var item in files)
            {
                if (item.ToLower().EndsWith(".xlsx"))
                {
                    currentExcel = item;
                    tubes.Clear();
                    tubes = await excelHandle.ReadExcel(currentExcel);
                    listViewShow.Items.Clear();
                    tubes.ForEach((t)=> {
                        listViewShow.Items.Add(t.ToListItem());


                    });
                    break;
                }

            }
           

        }

        private void label1_DragEnter(object sender, DragEventArgs e)
        {
            //验证是否是文件
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Move;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (tubes.Count>0)
            {
                var sysGroup = tubes.GroupBy(t => t.SYSTEM );

                foreach (var item in sysGroup)
                {
                    var group = tubes.Where(t=>t.SYSTEM==item.Key);

                    Write(item.Key,group);

                }
            }
        }
        private void Write(string sys, IEnumerable<Tube> tubes)
        {
            string template = "{0}={1}";
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("[Associations]");
            
            sb.AppendLine(string.Format(template, "SERVICE", sys));
            
            sb.AppendLine("UNIT=" + "CP5");
           
            sb.AppendLine("");

            foreach (Tube tube in tubes)
            {
                sb.AppendLine("[InstanceData]");
                foreach (PropertyInfo p in tube.GetType().GetProperties())
                {
                    if (p.Name!= "SYSTEM")
                    {
                        string temp = string.Format(template, p.Name, p.GetValue(tube));

                        sb.AppendLine(temp);

                    }
                   
                }
                sb.AppendLine();
               
            }
            File.WriteAllText(sys + ".txt", sb.ToString(), Encoding.UTF8);

        }
    }
}
